import {listType} from "@/api/system/dict/type";

/**
 * 表单管理状态管理
 * @type {{}}
 */
const state = {
  activeId:"",
  activeData:{},
  layouts:[],
  formConfig:{
    labelPosition:"top"
  },
  dictList:[]
}

const mutations = {
  ADD_COMPONENT:(state,component)=>{
    state.activeId = component.id;
    state.activeData = component;
    state.layouts.push(component);
  },
  SET_COMPONENT:(state,component)=>{
    state.activeId = component.id;
    state.activeData = component;
  },
  CLEAN_COMPONENT:(state)=>{
    state.activeId = "";
    state.activeData = {};
  },
  ADD_DICT:(state)=>{
    if(state.dictList.length){
      return;
    }else{
      listType({
        pageNum: 1,
        pageSize: 1000
      }).then(res=>{
        state.dictList = res.rows;
      })
    }

  },
  SET_LAYOUTS:(state,config)=>{
    if(Array.isArray(config.layouts)){
      state.layouts = config.layouts
    }
    state.formConfig = config.formConfig
  }
}

const actions = {
  ADD_COMPONENT:({commit},component)=>{
    commit('ADD_COMPONENT',component)
  },
  SET_COMPONENT:({commit},component)=>{
    commit('SET_COMPONENT',component)
  },
  CLEAN_COMPONENT:({commit},component)=>{
    commit('CLEAN_COMPONENT',component)
  },
  ADD_DICT:({commit})=>{
    commit('ADD_DICT')
  },
  SET_LAYOUTS:({commit},config)=>{
    commit('SET_LAYOUTS',config)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
